import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';

import { JwtHelperService } from '@auth0/angular-jwt';

export interface UserInfo {
  userid: number;
  username: string;
  userentity: {
    authcode: string[];
  };
}

@Injectable({
  providedIn: 'root'
})
export class UserInfoService {
  private userInfo$ = new BehaviorSubject<UserInfo>({ userid: -1, username: '', userentity: { authcode: [] } });

  // parsToken(token: string): UserInfo {
  //   const helper = new JwtHelperService();
  //   try {
  //     const { Id: id } = helper.decodeToken(token);
  //     return {
  //       id,
  //       authCode: []
  //     };
  //   } catch (e) {
  //     return {
  //       id: -1,
  //       authCode: []
  //     };
  //   }
  // }

  setUserInfo(userInfo: UserInfo): void {
    this.userInfo$.next(userInfo);
  }

  getUserInfo(): Observable<UserInfo> {
    return this.userInfo$.asObservable();
  }
}
